package cn.huiyunche.base.service.interfaces.impl;

import cn.huiyunche.base.service.interfaces.SOrderTrailerService;
import cn.huiyunche.base.service.mappers.SOrderTrailerMapper;
import cn.huiyunche.base.service.model.SOrderTrailer;
import cn.huiyunche.base.service.model.SOrderTrailerExample;
import org.apache.commons.collections4.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @FileName: cn.huiyunche.app.service.interfaces.impl
 * @Description: Description
 * @author: Aaron
 * @date: 2016/11/18 上午11:26
 */
@Service
public class SOrderTrailerServiceImpl implements SOrderTrailerService {

    private static final Logger LOGGER = LoggerFactory.getLogger(SOrderTrailerServiceImpl.class);

    @Autowired
    private SOrderTrailerMapper sOrderTrailerMapper;

    @Override
    public void add(Integer trailerId, Long sOrderId) {
        LOGGER.info("add params : {}, {}", trailerId, sOrderId);
        if (null == trailerId) {
            LOGGER.error("add param trailerId must not be null");
            throw new IllegalArgumentException("板车类型主键不能为空");
        }
        if (null == sOrderId) {
            LOGGER.error("add param sOrderId must not be null");
            throw new IllegalArgumentException("客户订单主键不能为空");
        }

        SOrderTrailer trailer = new SOrderTrailer();
        trailer.setbTrailerTypeId(trailerId);
        trailer.setsOrderId(sOrderId);

        sOrderTrailerMapper.insertSelective(trailer);
    }

    @Override
    public SOrderTrailer getBySOrderId(Long sOrderId) {
        LOGGER.error("getBySOrderId param : {}", sOrderId);
        if (null == sOrderId) {
            LOGGER.error("getBySOrderId param sOrderId must not be null");
            throw new IllegalArgumentException("客户订单主键不能为空");
        }

        SOrderTrailerExample example = new SOrderTrailerExample();
        example.createCriteria().andSOrderIdEqualTo(sOrderId);

        List<SOrderTrailer> list = sOrderTrailerMapper.selectByExample(example);
        if (CollectionUtils.isNotEmpty(list))
            return list.get(0);

        return null;
    }
}
